package servlet.user;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Event;

import managers.EntityFunctionManager;

/**
 * Servlet implementation class SearchEvent
 */
public class SearchEvent extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchEvent() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @EJB EntityFunctionManager mng;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String text = request.getParameter("text");
		String search = request.getParameter("search");
		
		if(!text.isEmpty() && !search.isEmpty()) {
		
			if(search.equals("Name")) {
				List<Event> e = mng.searchEventByName(text);
				String goTo="EventList.jsp";
				request.setAttribute("data", e);
				request.getRequestDispatcher(goTo).forward(request, response);
			}
			else {
				if(search.equals("Place")){
					List<Event> e =mng.searchEventByPlace(text);
					String goTo="EventList.jsp";
					request.setAttribute("data", e);
					request.getRequestDispatcher(goTo).forward(request, response);
				} 
				else {
					if(search.equals("Hour")) {
					
						List<Event> e =mng.searchEventByHour(text);
						String goTo="EventList.jsp";
						request.setAttribute("data", e);
						System.out.println("Not error asasa");
						request.getRequestDispatcher(goTo).forward(request, response);
					}
					else { //Promoter
						if(search.equals("Date")) {
							List<Event> e =mng.searchEventByDate(text);
							String goTo="EventList.jsp";
							request.setAttribute("data", e);
							System.out.println("Not error asasa");
							request.getRequestDispatcher(goTo).forward(request, response);
						}
						
						else {
							List<Event> e =mng.searchEventByPromoter(text);
						String goTo="EventList.jsp";
						request.setAttribute("data", e);
						System.out.println("Not error asasa");
						request.getRequestDispatcher(goTo).forward(request, response);
						}
					}
				}
			}
		}
		
		else {
			String goTo="Error.jsp";
			request.getRequestDispatcher(goTo).forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
